如何在保持代码的递归性质的同时使此函数线程安全?
int foo(char *p)
{
static int i = 0;
if (*p == '\0') return i;
i++;
return foo(p+1);
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
using namespace std;
int foo(char* p, int start)
{
if (*p == 0) return start;
return foo(p+1, start+1);
}
int main()
{
char test[] = "HI THERE";
cout << foo(test, 0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)