I have no experience in parallel search and never learned about using it in C.
What are the commands to use parallel search in C?
Can I get an example to some simple C program that use parallel search
to do some computations in the same time(It does not have to be a chess program).
For example a program that calculate the sum of the numbers in a big array faster.
Using more than 1 thread in C beginner question.
Moderators: hgm, Rebel, chrisw
-
- Posts: 10302
- Joined: Thu Mar 09, 2006 12:37 am
- Location: Tel-Aviv Israel
-
- Posts: 2204
- Joined: Sat Jan 18, 2014 10:24 am
- Location: Andorra
Re: Using more than 1 thread in C beginner question.
I use only this instruction for Andscacs. All the other things are communication through variables.
Code: Select all
_beginthread(proces_thread, 0, (void*)&rootparam.numthread[i]);
void proces_thread(void *param) {
int *threadnum= (int*)param;
...
Daniel José - http://www.andscacs.com
-
- Posts: 2559
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Using more than 1 thread in C beginner question.
If your compiler supports OpenMP, you can start with
(assuming each iteration takes roughly same time)
Code: Select all
#pragma omp parallel for
for(...) {
}
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Using more than 1 thread in C beginner question.
Here is a simple idea using posix threads in Linux:Uri Blass wrote:I have no experience in parallel search and never learned about using it in C.
What are the commands to use parallel search in C?
Can I get an example to some simple C program that use parallel search
to do some computations in the same time(It does not have to be a chess program).
For example a program that calculate the sum of the numbers in a big array faster.
main program:
int global_sum = 0;
pthread_create(process1, sumarray, 0);
pthread_create(process2, sumarray, 1);
pthread_join(process1);
pthread_join(process2);
done...
int sumarray(my_id) {
int i, start, end, sum = 0;
start = size_of_array * my_id / 2;
end = start + size_of_array / 2;
for (i = start; i < end; i++)
sum += array;
lock(sum_lock)
global_sum += sum;
unlock(sum_lock);
return 0;
}
That is sort of the idea in pseudo-code that resembles C.
pthread_create creates a new process and provides you with an id that you can then use to wait for that process to complete (pthread_join does this). The sumarray program just sums 1/2 of the array, where thread zero goes 0 to size / 2, thread one goes size / 2 to size. Then, at the end, you acquire a lock so that both threads don't try to update global_sum at the same time, then add the partial sum to that...
Hope that helps...
-
- Posts: 10302
- Joined: Thu Mar 09, 2006 12:37 am
- Location: Tel-Aviv Israel
Re: Using more than 1 thread in C beginner question.
Thanks for all the replies
-
- Posts: 3232
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: Using more than 1 thread in C beginner question.
C99 (or previous) has no support for multi threading. You need to do everything yourself by calling the relevant OS API. And you need to handle POSIX threads or Windows threads differently, of course...Uri Blass wrote:I have no experience in parallel search and never learned about using it in C.
What are the commands to use parallel search in C?
Can I get an example to some simple C program that use parallel search
to do some computations in the same time(It does not have to be a chess program).
For example a program that calculate the sum of the numbers in a big array faster.
C11 has thread support, just like C++11. However, C11 threads only exist on paper. In reality, not even glibc support C11 threads. So you're back to square one...
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
-
- Posts: 151
- Joined: Thu Nov 12, 2009 6:31 pm
Re: Using more than 1 thread in C beginner question.
Under linux, at least one libc claims to support c11 threads:lucasart wrote:C11 has thread support, just like C++11. However, C11 threads only exist on paper. In reality, not even glibc support C11 threads. So you're back to square one...
http://www.musl-libc.org/
Under windows some 3rd parties may provide support to C11 threads, (pelle C ?, ...), but I have not tested them (I do not use ms windows anymore).
IMHO, the easiest way to have a portable threading support in C is to use Posix threads. It is native under Linux, works under windows with the pthread library add-on. It also mostly works natively under Apple OS X.
Richard