Using more than 1 thread in C beginner question.

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
Uri Blass
Posts: 8591
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Using more than 1 thread in C beginner question.

Post by Uri Blass » Mon Jan 11, 2016 1:34 pm

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.

User avatar
cdani
Posts: 2104
Joined: Sat Jan 18, 2014 9:24 am
Location: Andorra
Contact:

Re: Using more than 1 thread in C beginner question.

Post by cdani » Mon Jan 11, 2016 2:06 pm

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;
...

mar
Posts: 2001
Joined: Fri Nov 26, 2010 1:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Using more than 1 thread in C beginner question.

Post by mar » Mon Jan 11, 2016 5:35 pm

If your compiler supports OpenMP, you can start with

Code: Select all

#pragma omp parallel for
for(...) {
}
(assuming each iteration takes roughly same time)

bob
Posts: 20559
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: Using more than 1 thread in C beginner question.

Post by bob » Mon Jan 11, 2016 5:36 pm

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.
Here is a simple idea using posix threads in Linux:

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...

Uri Blass
Posts: 8591
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Re: Using more than 1 thread in C beginner question.

Post by Uri Blass » Mon Jan 11, 2016 7:53 pm

Thanks for all the replies

User avatar
lucasart
Posts: 3041
Joined: Mon May 31, 2010 11:29 am
Full name: lucasart
Contact:

Re: Using more than 1 thread in C beginner question.

Post by lucasart » Mon Jan 11, 2016 11:15 pm

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.
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...

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.

abulmo
Posts: 151
Joined: Thu Nov 12, 2009 5:31 pm
Contact:

Re: Using more than 1 thread in C beginner question.

Post by abulmo » Tue Jan 12, 2016 6:35 am

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...
Under linux, at least one libc claims to support c11 threads:
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

Post Reply