succeded in calling api from main thread but not in child thread

Bvm 18
Bvm 18 used Ask the Experts™

   I have called third-party  api from main thread in c++ on linux which is working fine as expected. But, when I call same api from child thread, it's failing in multi threaded application.

   technically what could be the reason? (not interested to check with third-party people).

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi Bvm,

Any of several things could be in play.  

- The API may not be thread safe.
- The API class object may not be thread safe.
- Does the API class require instantiation and if so how does it relate to the threads?

evilrixSenior Software Engineer (Avast)

Please define failing. Crashing? Just doing nothing? API returns an error? Doesn't compile?

Please define this and provide any relevent error output.

Knowing the API would also be useful.
David FavorFractional CTO
Distinguished Expert 2018

Far more detail is required.

First item required is to clearly describe what an API call means.

1) If you're calling some external site via an HTTPS POST call, then your curl library or other library you're using must be thread safe.

2) If you're calling some function library provided by a 3rd party, then this will almost always fail because very few SDKs provide contain thread safe call.

The general... bottom line... here relates to, if you're calling code from a thread all code implicitly/explicitly included in all call chains must be designed from the start to be thread safe.

Thread safety, as Kent implied, must be... baked in to work...
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT

@Bvm 18

Proceed based on comment from Kent Olsen.

Tried following sample code:
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <iostream>
using namespace std;
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *worker_thread(void *arg)
	if ( NULL != arg)
		cout << "worker_thread " << (char*)arg << " PID: " << getpid() << " PPID: " << getppid() << "\n";
int main()
	pthread_t my_thread = 0;
	char *data = NULL;
	int ThreadRet = 0;
	if ( 0 == fork() )
		data = (char*)"CHILD";
		cout << "main1         " << (char*)data << " PID: " << getpid() << " PPID: " << getppid() << "\n";
		ThreadRet =  pthread_create(&my_thread, NULL, worker_thread, (void*)data);
		if( 0 != ThreadRet )
			cout << "Error: pthread_create() failed\n";
		// Following never reached since CHILD process has been replaced by thread.
		cout << "main2         " << (char*)data << " PID: " << getpid() << " PPID: " << getppid() << "\n";
		data = (char*)"PARENT";
		cout << "main3         " << (char*)data << " PID: " << getpid() << " PPID: " << getppid() << "\n";
	return 0;

Open in new window

$ /usr/bin/g++ -Wall 29158080.cpp -o ./a.out
$ ./a.out
main1         CHILD PID: 3084 PPID: 3083
worker_thread CHILD PID: 3084 PPID: 3083
main3         PARENT PID: 3083 PPID: 91

1) Always initialize the variables.
2) 0 == fork() => Assign that to a variable and validate exceptions
3) better to use if ( 0 == varaible) => since some one may write if ( variable = 0 ) => assigning zero to variable
4) you can also include => signal(SIGCHLD, SIG_IGN) to ignore SIGCHLD
David FavorFractional CTO
Distinguished Expert 2018

Aside: About threaded apps calling HTTPS APIs.

1) Threaded code is complex + fragile. Far more difficult to write than single threaded code.

2) Using C++ requires error prone memory management.

3) The point of using C++ along with multi-threading is usually to increase performance.

There is no way to increase performance of code making HTTPS API calls, as your bottleneck is always the physical Ethernet adapter + speed of connection.

This means there's no point for using C++ or multi-threading.

Better to write a standalone (command line) script in PERL (or any scripting language with rock solid memory management) to make your HTTPS API calls + pass back the JSON or other payload to your C++ code.

If you take this approach you can also debug your HTTPS API call script independently of your C++ multi-threaded mess.

As Kent suggested, the reason for your code failure is almost certainly due to some non thread-safe code being called.

To figure this out, you'll have to do a large amount of research + debugging, depending on all the code involved.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial