srjacob
Posts: 39
Joined: Sun Jun 10, 2018 3:21 pm

Mutex format for C++

Sun Jul 08, 2018 7:55 pm

I am a newbe to linux.

I want to synchronize several threads with mutex (or some other semaphore) in C++. All the references I have found show a slightly different format of the syntax. So far, I haven't found a good C++ reference for Raspian. It is somewhat maddening.

Could some kind soul please give me an example of using mutex in a C++ program on Raspian. Either that, or point to a good reference.

Thanks in advance.

Steve

User avatar
joan
Posts: 13457
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Mutex format for C++

Sun Jul 08, 2018 8:53 pm

Raspbian is one of many Linux/GNU distributions. It doesn't make sense to look for Raspbian documentation for C++ or mutexes. Look for Linux mutexes.

User avatar
DougieLawson
Posts: 33348
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Mutex format for C++

Sun Jul 08, 2018 9:10 pm

Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Heater
Posts: 9473
Joined: Tue Jul 17, 2012 3:02 pm

Re: Mutex format for C++

Sun Jul 08, 2018 9:10 pm

C++ the language has no idea about threads. Not even in its standard library. Or at least the last time I checked that was so.

Raspbian is Linux. The standard threads library in Linux is pthreads (POSIX threads). See here: https://docs.oracle.com/cd/E19120-01/op ... index.html and a billion other places on the web.

If you happen to be using the Qt graphical user interface libraries then that wraps pthreads up nicely in a QThread class: http://doc.qt.io/qt-5/qthread.html

In fact I have written non-graphical applications using the Qt threads library with success.

No wait: C++ does have treads in its standard library: http://www.cplusplus.com/reference/thread/thread/

And mutexes and stuff http://www.cplusplus.com/reference/mutex/

Amazing what google can find now a days. http://www.bogotobogo.com/cplusplus/C11 ... thread.php

jahboater
Posts: 2782
Joined: Wed Feb 04, 2015 6:38 pm

Re: Mutex format for C++

Sun Jul 08, 2018 9:40 pm

Heater wrote:
Sun Jul 08, 2018 9:10 pm
C++ the language has no idea about threads. Not even in its standard library.
I think the presumption with C++ should be that it has the feature ....

There is also std::async (simple threads), std::memory_order (atomics) and probably lots of other stuff.

srjacob
Posts: 39
Joined: Sun Jun 10, 2018 3:21 pm

Re: Mutex format for C++

Mon Jul 09, 2018 1:35 am

Thank you for your responses. I am familiar with the CPP site. I tried some of those examples, and they don't compile on my PI.

I would like to think that C++ on all Linux distributions is the same, but it appears that there are some differences. I have several C++ reference texts, and they all differ somewhat when it comes to mutex.

The C++ implementation of threads is composed of class wrappers for the basic Linux (Posix) threads. The flavor of the wrappers seems to differ somwhat between flavors of Linux.

All I am asking for is this: I would like to see a mutex class definition (constructor) usage, and a usage of a mutex in a program. That's it. That's all I am asking for. The rest I can handle myself.

I am VERY familiar with threading on Windows. I am a newbe to Linux, however. Documentation on Windows is excellent, primarily because MS pays big bucks to develop it, so I guess I am spoiled. No one is going to pay big bucks to document Linux.

Heater
Posts: 9473
Joined: Tue Jul 17, 2012 3:02 pm

Re: Mutex format for C++

Mon Jul 09, 2018 4:42 am

srjacob,
I tried some of those examples, and they don't compile on my PI.
If you were to show your code here, along with your compile command and it's error messages no doubt I or someone here could help you fix it.
I would like to think that C++ on all Linux distributions is the same, but it appears that there are some differences.
C++ is very well standardized and the usual C++ compiler on Linux is GCC which implements that statndard very well. Any platform differences, 32 or 64 bit, bigendian or little endian, etc, should not make any difference for your threading example.
The C++ implementation of threads is composed of class wrappers for the basic Linux (Posix) threads. The flavor of the wrappers seems to differ somwhat between flavors of Linux.
I doubt it. If you are talking about the threads as in the C++ standard library, std::thread, I'm very sure it's the same on all Linux distros. It all comes with the same GCC and it's all from the same ISO standard.
Documentation on Windows is excellent, primarily because MS pays big bucks to develop it, so I guess I am spoiled. No one is going to pay big bucks to document Linux.
Oh but they do. pthreads is POSIX threads is an IEEE standard. posix threads work on Windows as well as Unix/Linux. The C++ standards thread library is part of an ISO standard. Which in turn is supported by the likes of Microsoft, IBM, etc.

This is an example of using the C++ threads library, including mutexs, which works:

Code: Select all

#include <iostream>
#include <thread>
#include <list>
#include <algorithm>
#include <mutex>

using namespace std;

// a global variable
std::list<int>myList;

// a global instance of std::mutex to protect global variable
std::mutex myMutex;

void addToList(int max, int interval)
{
	// the access to this function is mutually exclusive
	std::lock_guard<std::mutex> guard(myMutex);
	for (int i = 0; i < max; i++) {
		if( (i % interval) == 0) myList.push_back(i);
	}
}

void printList()
{
	// the access to this function is mutually exclusive
	std::lock_guard<std::mutex> guard(myMutex);
	for (auto itr = myList.begin(), end_itr = myList.end(); itr != end_itr; ++itr ) {
		cout << *itr << ",";
	}
}

int main()
{
	int max = 100;

	std::thread t1(addToList, max, 1);
	std::thread t2(addToList, max, 10);
	std::thread t3(printList);

	t1.join();
	t2.join();
	t3.join();

	return 0;
}
Compile like so:
$ g++ -std=c++11 -o thread-example thread-example.cpp -lpthread
Note the "-std" option that selects use of the C++ 11 standard which is when threads were introduced to C++. And the -lpthread option to pull in the pthreads library.

And run like so:

Code: Select all

$ ./thread-example
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,0,10,20,30,40,50,60,70,80,90,
$
This code will no doubt build and run on Windows as well so all your C++ on Windows experience can be brought over to Linux and the Pi.

The above example is from here: http://www.bogotobogo.com/cplusplus/C11 ... Memory.php


srjacob
Posts: 39
Joined: Sun Jun 10, 2018 3:21 pm

Re: Mutex format for C++

Mon Jul 09, 2018 1:15 pm

OK. Here is my problem:

std::mutex TopButton, BottomButton; // sync mutex for top and bottom button

ERROR: ]/home/pi/DRHE/main.cpp:64:6: error: ?mutex? in namespace ?std? does not name a type

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 20064
Joined: Sat Jul 30, 2011 7:41 pm

Re: Mutex format for C++

Mon Jul 09, 2018 1:17 pm

I presume you have

Code: Select all

#include <mutex>
at the top of the file?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Please direct all questions to the forum, I do not do support via PM.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 5 guests