roycejp
Posts: 19
Joined: Sun Oct 27, 2013 7:48 am

WiringPi: question about multiThread program

Mon Mar 31, 2014 12:37 pm

Hello,

I have made a C program under gcc using wiringPi, which is working well.

I want tp try multi threading to improve performance of certain part of my code.

I'm not clear about the piLock / piUnLock functions.

To quote from the wiringPi reference page,

Code: Select all

piLock (int keyNum) ;
piUnlock (int keyNum) ;
These allow you to synchronise variable updates from your main program to any threads running in your program. keyNum is a number from 0 to 3 and represents a “key”. When another process tries to lock the same key, it will be stalled until the first process has unlocked the same key.


So does this mean I can have only 4 (0 to 3) shared variables between my 'main' and all another threads ?
How to handle more shared variables ?

Can anyone shed some light ?

Thanks !

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: WiringPi: question about multiThread program

Mon Mar 31, 2014 6:26 pm

roycejp wrote:Hello,

I have made a C program under gcc using wiringPi, which is working well.

I want tp try multi threading to improve performance of certain part of my code.

I'm not clear about the piLock / piUnLock functions.

To quote from the wiringPi reference page,

Code: Select all

piLock (int keyNum) ;
piUnlock (int keyNum) ;
These allow you to synchronise variable updates from your main program to any threads running in your program. keyNum is a number from 0 to 3 and represents a “key”. When another process tries to lock the same key, it will be stalled until the first process has unlocked the same key.


So does this mean I can have only 4 (0 to 3) shared variables between my 'main' and all another threads ?
How to handle more shared variables ?

Can anyone shed some light ?

Thanks !
You can have millions - they're just there, like a lot of wiringPi as "helpers". To make life easy for you.

If you want more, just look at the source code, (piThread.c) or type: man pthread_mutex_lock

-Gordon
--
Gordons projects: https://projects.drogon.net/

roycejp
Posts: 19
Joined: Sun Oct 27, 2013 7:48 am

Re: WiringPi: question about multiThread program

Tue Apr 01, 2014 5:51 am

Thank You.

So to increase the number of shared variables I edit this line:

Code: Select all

static pthread_mutex_t piMutexes [4] ;
and change the '4' to any value I want ?

BTW,

Code: Select all

man pthread_mutex_lock 
gives me:

Code: Select all

No manual entry for pthread_mutex_lock

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: WiringPi: question about multiThread program

Tue Apr 01, 2014 10:23 am

roycejp wrote:Thank You.

So to increase the number of shared variables I edit this line:

Code: Select all

static pthread_mutex_t piMutexes [4] ;
and change the '4' to any value I want ?
Yes - however I'd suggest that if you're at that level, then you might want to look at using the functions directly.

BTW,

Code: Select all

man pthread_mutex_lock 
gives me:

Code: Select all

No manual entry for pthread_mutex_lock
I think the manual pages might not be installed by default. (as not everyone does C programming on the Pi) Try:

Code: Select all

sudo apt-get install glibc-doc
The whole package is called pthreads:

Code: Select all

man pthreads
Happy reading!

-Gordon
--
Gordons projects: https://projects.drogon.net/

roycejp
Posts: 19
Joined: Sun Oct 27, 2013 7:48 am

Re: WiringPi: question about multiThread program

Tue Apr 01, 2014 7:54 pm

Things are not going all that well ...

I'm getting this error (where 'webradio' is the name of my executable) :

Code: Select all

*** glibc detected *** ./webradio: double free or corruption (top): 0x00662228 ***
and it aborts.

I'm using 2 threads besides main(), and 2 mutexes in the application.

What does this mean ?

Thank You.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: WiringPi: question about multiThread program

Tue Apr 01, 2014 8:00 pm

roycejp wrote:Things are not going all that well ...

I'm getting this error (where 'webradio' is the name of my executable) :

Code: Select all

*** glibc detected *** ./webradio: double free or corruption (top): 0x00662228 ***
and it aborts.

I'm using 2 threads besides main(), and 2 mutexes in the application.

What does this mean ?

Thank You.
It means you have a bug in your program - probably calling free() more than once on the same pointer. (or something is, if it's not you)

although wiringPi does use dynamic memory allocation (calloc), it never frees it up, so I'm fairly sure it's not wiringPi producing that error.

Compile it with the -g flag, enable core dumps, run it - get a core file, then run gdb on it and issue the 'where' command.

-Gordon
--
Gordons projects: https://projects.drogon.net/

roycejp
Posts: 19
Joined: Sun Oct 27, 2013 7:48 am

Re: WiringPi: question about multiThread program

Wed Apr 02, 2014 3:55 pm

I managed to track down the bug.
I was calling a shell command inside one thread using a file pointer which was also being used by the main thread.

I just created separate file pointer for the thread, and the problem went away.

Many thanks for your help !

I'll post further updates here :-)

I plan to eventually release my project to the public.

Return to “C/C++”