473,714 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multi-threaded C programming (for those fond of portability)


As far as I know, the C Standard has no mention of multi-threaded
programming; it has no mention of how to achieve multi-threaded
programming, nor does it mention whether the language or its libraries
are suitable for multi-threaded programming.

For people who are fond of portable C programming, what's the best way
to go about multi-threaded programming?

I've been reading up on POSIX threads a little, they seem pretty
ubiquitous. Would this be the best way to go?

One issue I've seen raised is about the "thread safety" of functions
in the Standard C library. Would it be fair to assume, on systems
where POSIX threads is implemented, that the Standard C library will
be thread-safe? Or is there a well-known way of asking the compiler to
ensure this, for example by defining the preprocessor directive
_REENTRANT?

I'm basically looking for the most portable way possible to write a
multi-threaded application.
Oct 29 '08 #1
14 3403
Tomás Ó hÉilidhe <to*@lavabit.co mwrites:
For people who are fond of portable C programming, what's the best way
to go about multi-threaded programming?
You have a set of reasonable questions, but this is not a good
place to ask them. If you ask them in comp.programmin g.threads,
instead, you are likely to receive better answers.
--
"Give me a couple of years and a large research grant,
and I'll give you a receipt." --Richard Heathfield
Oct 29 '08 #2
In article <cb************ *************** *******@v39g200 0pro.googlegrou ps.com>,
Tomas S hIilidhe <to*@lavabit.co mwrote:
>For people who are fond of portable C programming, what's the best way
to go about multi-threaded programming?
Pick a reasonably widely implemented standard that defines a threading
interface, write to that standard, and declare it "Portable to
platforms that support C + <other standard>".

>I've been reading up on POSIX threads a little, they seem pretty
ubiquitous. Would this be the best way to go?
I'm not qualified to comment on "best", but it's probably a rather good
way.

>One issue I've seen raised is about the "thread safety" of functions
in the Standard C library. Would it be fair to assume, on systems
where POSIX threads is implemented, that the Standard C library will
be thread-safe? Or is there a well-known way of asking the compiler to
ensure this, for example by defining the preprocessor directive
_REENTRANT?
I would assume that part of the specification of any sensible threading
API is additional requirements on the C standard library related to
multithreaded programs (what the implementation is required to support,
and what the programmer is forbidden from doing).

For POSIX, comp.unix.progr ammer is probably a good place to ask about
such things.
dave

--
Dave Vandervies dj3vande at eskimo dot com
But I don't daydream about being a [paramedic], because my subconscious knows
that learning to fly unassisted is more likely than my being capable of being
a paramedic. --David Cameron Staples in the scary devil monastery
Oct 29 '08 #3
Tomás Ó hÉilidhe writes:
For people who are fond of portable C programming, what's the best way
to go about multi-threaded programming?
I've been reading up on POSIX threads a little, they seem pretty
ubiquitous. Would this be the best way to go?
That or OpenMP, I guess. But as Ben said, goto comp.programmin g.threads.
One issue I've seen raised is about the "thread safety" of functions
in the Standard C library. Would it be fair to assume, on systems
where POSIX threads is implemented, that the Standard C library will
be thread-safe? Or is there a well-known way of asking the compiler to
ensure this, for example by defining the preprocessor directive
_REENTRANT?
_REENTRANT is in the compiler's namespace, so do not touch it unless
the compiler doc or some standard it follows says you should. There
will be some compiler option to make the compiler produce thread-safe
object code, link the right libraries, #define something like
_REENTRANT you can #ifdef on, etc. See the compiler's documentation
for that switch, don't try something just because it "seems to work".

--
Hallvard
Oct 29 '08 #4
On Oct 29, 2:20*pm, Tomás Ó hÉilidhe <t...@lavabit.c omwrote:
As far as I know, the C Standard has no mention of multi-threaded
programming; it has no mention of how to achieve multi-threaded
programming, nor does it mention whether the language or its libraries
are suitable for multi-threaded programming.

For people who are fond of portable C programming, what's the best way
to go about multi-threaded programming?

I've been reading up on POSIX threads a little, they seem pretty
ubiquitous. Would this be the best way to go?
On POSIX systems it is.
One issue I've seen raised is about the "thread safety" of functions
in the Standard C library. Would it be fair to assume, on systems
where POSIX threads is implemented, that the Standard C library will
be thread-safe?
No. On some systems the reentrant functions have different names.
Or is there a well-known way of asking the compiler to
ensure this, for example by defining the preprocessor directive
_REENTRANT?
I guess that it varies by compiler and platform.
I'm basically looking for the most portable way possible to write a
multi-threaded application.
For C++ there are plenty of solutions:
http://www.boost.org/doc/libs/1_36_0...ml/thread.html
http://www.cs.wustl.edu/~schmidt/ACE.html
http://www.inf.uni-konstanz.de/dbis/.../doc/info.html

For C, you can try:
http://download.imatix.com/pub/
http://www.cs.uoi.gr/~ompi/download.html#psthreads
http://sourceware.org/pthreads-win32/ and http://pauillac.inria.fr/~xleroy/linuxthreads/

But I doubt you will be happy with any of them.

I guess you will be stuck with C++ if you want something fairly
portable (E.g. Ace runs on everything under the sun, including Sun).
Here are the headers for various systems:
config-aix-5.x.h
config-all.h
config-borland-common.h
config-cray.h
config-cxx-common.h
config-cygwin32.h
config-doxygen.h
config-freebsd.h
config-g++-common.h
config-ghs-common.h
config-hpux-11.00.h
config-icc-common.h
config-integritySCA.h
config-irix6.5.x-sgic++.h
config-irix6.x-common.h
config-irix6.x-g++.h
config-irix6.x-sgic++.h
config-linux-common.h
config-linux.h
config-lite.h
config-lynxos.h
config-macosx-panther.h
config-macosx-tiger.h
config-macosx.h
config-macros.h
config-minimal.h
config-mvs.h
config-netbsd.h
config-openbsd.h
config-openvms.h
config-pharlap.h
config-posix-nonetworking.h
config-posix.h
config-qnx-neutrino.h
config-qnx-rtp-62x.h
config-qnx-rtp-common.h
config-qnx-rtp-pre62x.h
config-qnx-rtp.h
config-rtems.h
config-sco-5.0.0-nothread.h
config-sco-5.0.0.h
config-suncc-common.h
config-sunos5.10.h
config-sunos5.4-g++.h
config-sunos5.4-sunc++-4.x.h
config-sunos5.5.h
config-sunos5.6.h
config-sunos5.7.h
config-sunos5.8.h
config-sunos5.9.h
config-tandem-nsk-mips-v2.h
config-tandem-nsk-mips-v3.h
config-tandem.h
config-tru64.h
config-unixware-2.01-g++.h
config-unixware-2.1.2-g++.h
config-unixware-7.1.0.h
config-unixware-7.1.0.udk.h
config-visualage.h
config-vxworks5.x.h
config-vxworks6.2.h
config-vxworks6.3.h
config-vxworks6.4.h
config-vxworks6.5.h
config-win32-borland.h
config-win32-common.h
config-win32-dmc.h
config-win32-ghs.h
config-win32-interix.h
config-win32-mingw.h
config-win32-msvc-7.h
config-win32-msvc-8.h
config-win32-msvc-9.h
config-win32-msvc.h
config-win32.h
config-WinCE.h

You might try news:comp.progr amming.threads, but it seems to be aimed
mostly at threaded programming on POSIX platforms.

We use ACE here {C++}, but Boost {also C++} looks like a very viable
alternative, and I understand that Boost is going to become part of
the next C++ standard. Wow, is that sucker going to kill a few trees.
Oct 29 '08 #5
On 29 Oct 2008 at 21:29, dj******@csclub .uwaterloo.ca.i nvalid wrote:
Tomas S hIilidhe <to*@lavabit.co mwrote:
>>One issue I've seen raised is about the "thread safety" of functions
in the Standard C library. Would it be fair to assume, on systems
where POSIX threads is implemented, that the Standard C library will
be thread-safe?

I would assume that part of the specification of any sensible
threading API is additional requirements on the C standard library
related to multithreaded programs (what the implementation is required
to support, and what the programmer is forbidden from doing).
Yes. Specifically, all standard library functions are guaranteed to be
threadsafe, with a short list of exceptions:

asctime()
basename()
catgets()
crypt()
ctime()
dbm_clearerr()
dbm_close()
dbm_delete()
dbm_error()
dbm_fetch()
dbm_firstkey()
dbm_nextkey()
dbm_open()
dbm_store()
dirname()
dlerror()
drand48()
ecvt()
encrypt()
endgrent()
endpwent()
endutxent()
fcvt()
ftw()
gcvt()
getc_unlocked()
getchar_unlocke d()
getdate()
getenv()
getgrent()
getgrgid()
getgrnam()
gethostbyaddr()
gethostbyname()
gethostent()
getlogin()
getnetbyaddr()
getnetbyname()
getnetent()
getopt()
getprotobyname( )
getprotobynumbe r()
getprotoent()
getpwent()
getpwnam()
getpwuid()
getservbyname()
getservbyport()
getservent()
getutxent()
getutxid()
getutxline()
gmtime()
hcreate()
hdestroy()
hsearch()
inet_ntoa()
l64a()
lgamma()
lgammaf()
lgammal()
localeconv()
localtime()
lrand48()
mrand48()
nftw()
nl_langinfo()
ptsname()
putc_unlocked()
putchar_unlocke d()
putenv()
pututxline()
rand()
readdir()
setenv()
setgrent()
setkey()
setpwent()
setutxent()
strerror()
strtok()
ttyname()
unsetenv()
wcstombs()
wctomb()

The usual convention is that a corresponding reentrant function is
provided, whose name is the standard name with a _r suffix (e.g.
rand_r). These versions often take an extra "state" parameter.

Oct 30 '08 #6
Tomás Ó hÉilidhe wrote:
>
As far as I know, the C Standard has no mention of multi-threaded
programming; it has no mention of how to achieve multi-threaded
programming, nor does it mention whether the language or its
libraries are suitable for multi-threaded programming.
Correct. The C standard does not deal with threads.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Oct 30 '08 #7
On Oct 29, 4:20*pm, Tomás Ó hÉilidhe <to*@lavabit.co mwrote:
As far as I know, the C Standard has no mention of multi-threaded
programming; it has no mention of how to achieve multi-threaded
programming, nor does it mention whether the language or its libraries
are suitable for multi-threaded programming.

For people who are fond of portable C programming, what's the best way
to go about multi-threaded programming?
There was recently a thread regarding this subject:
http://groups.google.com/group/comp....e1bcedd4fab1f#

The thing to do if you want to have a portable multi-threaded
application in C (or any language that doesn't support threads) is to
use a library that provides a single interface to whatever threading
environment the underlying OS supports.
I've been reading up on POSIX threads a little, they seem pretty
ubiquitous. Would this be the best way to go?
Only if your application is expected to run only on POSIX systems.
One issue I've seen raised is about the "thread safety" of functions
in the Standard C library. Would it be fair to assume, on systems
where POSIX threads is implemented, that the Standard C library will
be thread-safe? Or is there a well-known way of asking the compiler to
ensure this, for example by defining the preprocessor directive
_REENTRANT?
The C standard is very careless about some security issues, including
the issue of thread-safety (though I've heard that the C++0x standard
will indeed provide support for threads). Some functions in the
standard library are thread-safe, some are not (and it can be
difficult to figure out which are and which aren't). POSIX offers
thread-safe equivalents to functions in the C standard library that
aren't thread-safe (these usually have an "_r" suffix), for example:
strtok_r(), localtime_r() (and other functions in <time.h>). Reading
the man pages for the functions you're using can give clues about
whether they are thread-safe or not, since they also document POSIX
functions, and POSIX does address thread-safety.
I'm basically looking for the most portable way possible to write a
multi-threaded application.
There are tons of frameworks offering portable interfaces.

Sebastian

Oct 30 '08 #8
Tomás Ó hÉilidhe wrote:
>
I'm basically looking for the most portable way possible to write a
multi-threaded application.
Use C++...

--
Ian Collins
Oct 30 '08 #9

"Tomás Ó hÉilidhe" <to*@lavabit.co mwrote in message
news:cb******** *************** ***********@v39 g2000pro.google groups.com...
>
As far as I know, the C Standard has no mention of multi-threaded
programming; it has no mention of how to achieve multi-threaded
programming, nor does it mention whether the language or its libraries
are suitable for multi-threaded programming.

For people who are fond of portable C programming, what's the best way
to go about multi-threaded programming?

I've been reading up on POSIX threads a little, they seem pretty
ubiquitous. Would this be the best way to go?

One issue I've seen raised is about the "thread safety" of functions
in the Standard C library. Would it be fair to assume, on systems
where POSIX threads is implemented, that the Standard C library will
be thread-safe? Or is there a well-known way of asking the compiler to
ensure this, for example by defining the preprocessor directive
_REENTRANT?

I'm basically looking for the most portable way possible to write a
multi-threaded application.
Other people have covered this but,
you should also keep in mind that threads of the same application will never
execute simulatenously, even on multicore processors.
Oct 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
14571
by: OutsiderJustice | last post by:
Hi All, I can not find any information if PHP support multi-thread (Posix thread) or not at all, can someone give out some information? Is it supported? If yes, where's the info? If no, is it possible to make doing multi-thread stuff? Thanks. YF
6
7445
by: Thomas Womack | last post by:
If I have a dual-processor hyperthreaded machine (so with four CPU contexts), will a python program distribute threads over all four logical processors? I ask because I'm fairly sure that this *does* happen using the threading extensions in MFC, and fairly sure that it *doesn't* when using Java, so I don't see that the result is obvious for python. Tom
4
2419
by: Neil Zanella | last post by:
Hello, I would be very interested in knowing how the following C++ multi-instance singleton (AKA Borg) design pattern based code snippet can be neatly coded in Python. While there may be somewhat unusual places where multi-instance singleton is more useful than plain singleton, it seems to me that the former leads to less coding, so unless I can somehow package the singleton pattern in a superclass (so I don't have to code it...
37
4889
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours, Pujo
0
1236
by: Mike | last post by:
I have just installed Visual Studio .NET and all necessary Windows Updates. I can open my project in the IDE without any problems and can run it without any problems. My PC's OS is Windows 2000 Professional. When I try to multi- select controls (i.e., holding down the Ctrl key on the keyboard and left-click each control with the mouse) on any of my web forms, Internet Explorer opens. I have checked all my settings and searched MSDN...
5
8501
by: PEK | last post by:
I need some code that convert a multi-byte string to a Unicode string, and Unicode to multi-byte. I work mostly in Windows and know how to solve it there, but I would like to have some platform independent code too. I have tried with mbtowcs/wctombs but I'm not satisfied with the result. If wctombs finds a character that can't be converted it return -1, and stops. I would like to replace such of characters with some special character...
6
4891
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
3
3291
by: kathy | last post by:
I have a general question. If I write a application using any language(C/C++/C#/VB). How to make sure it only has one instance running? What I need to write in source code? If multi-instances run. Is any data confilct exsisted? Assuming I put the .exe in shared network drive and the multi-instances run on different PC. How about the multi-instances run on same PC?
5
3282
by: dkelly925 | last post by:
Is there a way to add an If Statement to the following code so if data in a field equals "x" it will launch one report and if it equals "y" it would open another report. Anyone know how to modify this? Private Sub cmdPreview_Click() On Error GoTo Err_Handler 'Purpose: Open the report filtered to the items selected in the list box. 'Author: Allen J Browne, 2004. http://allenbrowne.com Dim varItem As Variant 'Selected items
1
9313
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.